package com.soulkun.smarthome.ui.main.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainHomeDeviceHistoryBinding
import com.soulkun.smarthome.databinding.AppFragmentMainHomeDeviceHistoryDataBinding
import com.soulkun.smarthome.databinding.AppFragmentMainHomeDeviceRecentBinding
import com.soulkun.smarthome.databinding.AppRecycleItemMainHomeDeviceHistoryDataListBinding
import com.soulkun.smarthome.ui.main.home.adapter.AppMainHomeDeviceHistoryDataListAdapter
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeDeviceViewModel
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeViewModel
import soulkun.library.helper.decoration.LinearSpaceItemDecorationHelper
import soulkun.library.mvvm.component.BaseDataBindingFragment

class AppMainHomeDeviceHistoryDataFragment(private val viewModel: AppMainHomeDeviceViewModel) :
    BaseDataBindingFragment<AppFragmentMainHomeDeviceHistoryDataBinding>() {

    private val adapter by lazy {
        AppMainHomeDeviceHistoryDataListAdapter(
            mContext,
            viewModel.deviceDataHistoryList
        ) { binding ->
            View.OnClickListener {
                val isExpand =
                    (binding as AppRecycleItemMainHomeDeviceHistoryDataListBinding).deviceDataHistoryVO!!.isExpand.get()
                if (!isExpand && binding.deviceDataHistoryVO!!.dataHistoryList.isEmpty()) {
                    viewModel.getDeviceDataHistory(binding.deviceDataHistoryVO!!)
                }
                binding.deviceDataHistoryVO!!.isExpand.set(!isExpand)
            }
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_home_device_history_data
    }

    override fun initView() {
        requireDataBinding().srlMainHomeDeviceHistoryDataContainerHistory.run {
            setRefreshHeader(ClassicsHeader(mContext))
            setOnRefreshListener {
                viewModel.deviceDataHistoryList.clear()
                viewModel.getDeviceAttributeList{
                    finishRefresh()
                }
            }
        }
        requireDataBinding().rvMainHomeDeviceHistoryDataListHistory.run {
            layoutManager = LinearLayoutManager(mContext)
            adapter = this@AppMainHomeDeviceHistoryDataFragment.adapter
            if (itemDecorationCount == 0) {
                addItemDecoration(LinearSpaceItemDecorationHelper(0,10,0,0,10))
            }
        }
    }

    override fun initData() {
        viewModel.getDeviceAttributeList{
        }
    }

    override fun initListener() {
    }

    override fun doOnCreate() {
    }

}